﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using JBeamLibrary;

namespace DriveBeamEditor.Controls.BeamNG
{
    public partial class GameControl : UserControl
    {
        #region Fields

        /// <summary>Game resolution within the control</summary>
        private Size _gameResolution = new Size(960, 540);

        /// <summary>Vehicle loaded by default when the game starts</summary>
        private string _defaultVehicle = "super";

        #endregion

        #region Properties

        /// <summary>Gets or sets the game resolution</summary>
        public Size GameResolution
        {
            get { return _gameResolution; }
            set
            {
                if (value == _gameResolution) return;
                _gameResolution = value;
                InitializeGame();
            }
        }

        /// <summary>Gets or sets the vehicle used when the game starts</summary>
        public string DefaultVehicle
        {
            get { return _defaultVehicle; }
            set { _defaultVehicle = value; }
        }

        #endregion

        #region CTORs

        public GameControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (DesignMode) return;

            InitializeGame();
        }

        #endregion

        #region Events

        private void menuRestartGame_Click(object sender, EventArgs e)
        {
            appControl.Restart();
        }

        private void menuRefreshGame_Click(object sender, EventArgs e)
        {
            appControl.RefreshControl();
        }

        private void menuGameResolution_Click(object sender, EventArgs e)
        {
            var toks = ((ToolStripMenuItem)sender).Tag.ToString().Split('x');
            var width = int.Parse(toks[0]);
            var height = int.Parse(toks[1]);
            GameResolution = new Size(width, height);
        }

        #endregion

        #region Private methods

        private void InitializeGame()
        {
            if (string.IsNullOrEmpty(Config.GameConfigScriptFullPath)) return;

            /* Save config script */
            var origScript = string.Format("{0}.orig", Config.GameConfigScriptFullPath);
            File.Copy(Config.GameConfigScriptFullPath, origScript, true);
            File.Delete(Config.GameConfigScriptFullPath);

            /* Configure the game for embedded mode */
            var newLines = new List<string>();
            foreach (var line in File.ReadAllLines(origScript))
            {
                if (line.StartsWith(@"$pref::Video::borderless"))
                    newLines.Add("$pref::Video::borderless = \"1\";");
                else if (line.StartsWith(@"$pref::Video::mode"))
                    newLines.Add(string.Format("$pref::Video::mode = \"{0} {1} false 32 60 4\";", GameResolution.Width, GameResolution.Height));
                else
                    newLines.Add(line);
            }
            File.AppendAllLines(Config.GameConfigScriptFullPath, newLines);

            /* Use the game as executable */
            appControl.ExeName = string.Format("\"{0}\" -no3dgui -vehicle {1} -level GridMap.mis", Config.GameExeFullPath, DefaultVehicle);
        }

        private void ResetGameConfiguration()
        {
            if (DesignMode) return;

            /* Restore original configuration */
            var origScript = string.Format("{0}.orig", Config.GameConfigScriptFullPath);
            File.Copy(origScript, Config.GameConfigScriptFullPath, true);
            File.Delete(origScript);
        }

        #endregion
    }
}
